$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'order/list',
        datatype: "json",
        colModel: [
            {label: '订单id', name: 'id', width: 40},
            {
                label: '订单来源', name: 'orderSource', index: 'order_source', width: 40,
                formatter: function (value, options, row) {
                    if (value == 1) {
                        return '<span class="label label-info">堂食点餐</span>';
                    } else if (value == 2) {
                        return '<span class="label label-success">扫码点餐</span>';
                    } else {
                        return "";
                    }
                }
            },
            {label: '餐桌编号', name: 'tableNum', index: 'table_num', width: 20},
            {label: '商品总数', name: 'goodsNum', index: 'goods_num', width: 40},
            {label: '用餐人数', name: 'dinnerNum', index: 'dinner_num', width: 40},
            {label: '操作员id', name: 'oprateId', index: 'oprate_id', width: 40},
            {
                label: '总金额',
                name: 'totalAmt',
                index: 'total_amt',
                width: 40,
                formatter: function (value, options, row) {
                    var total_p = value / 100;
                    var resTotal = accounting.formatMoney(total_p, "", 2);
                    resTotal = resTotal.replace(".00", "");
                    return resTotal;
                }
            },
            {
                label: '总优惠金额',
                name: 'discountAmt',
                index: 'discount_amt',
                width: 40,
                formatter: function (value, options, row) {
                    var total_p = value / 100;
                    var resTotal = accounting.formatMoney(total_p, "", 2);
                    resTotal = resTotal.replace(".00", "");
                    return resTotal;
                }
            },
            {
                label: '实际支付金额',
                name: 'payAmt',
                index: 'pay_amt',
                width: 40,
                formatter: function (value, options, row) {
                    var total_p = value / 100;
                    var resTotal = accounting.formatMoney(total_p, "", 2);
                    resTotal = resTotal.replace(".00", "");
                    return resTotal;
                }
            },
            {
                label: '支付状态', name: 'payStatus', index: 'pay_status', width: 40,
                formatter: function (value, options, row) {
                    if (value == 0) {
                        return '<span class="label label-info">待支付</span>';
                    } else if (value == 1) {
                        return '<span class="label label-success">支付成功</span>';
                    } else if (value == 2) {
                        return '<span class="label label-warning">支付失败</span>';
                    } else if (value == 3) {
                        return '<span class="label label-warning">订单取消</span>';
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '支付方式', name: 'payType', index: 'pay_type', width: 40,
                formatter: function (value, options, row) {
                    if (value == 1) {
                        return '<span class="label label-info">微信</span>';
                    } else if (value == 2) {
                        return '<span class="label label-success">支付宝</span>';
                    } else if (value == 3) {
                        return '<span class="label label-warning">现金</span>';
                    } else {
                        return "";
                    }
                }
            },
            {
                label: '使用优惠券',
                name: 'couponFlag',
                index: 'coupon_flag',
                width: 40,
                formatter: function (value, options, row) {
                    if (value == 0) {
                        return '<span class="label label-info">未使用</span>';
                    } else if (value == 1) {
                        return '<span class="label label-success">已使用</span>';
                    } else {
                        return "";
                    }
                }
            },
            {label: '支付时间', name: 'payTime', index: 'pay_time', width: 40}
        ],
        viewrecords: true,
        height: 720,
        rowNum: 19,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        },
        ondblClickRow: function (id) {
            vm.detal(id);
        }
    });
});

var vm = new Vue({
    el: '#dcapp',
    data: {
        q: {
            key: null,
        },
        showList: true,
        title: null,
        order: {},
        orderDetails: [],
    },
    created: function () {

    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            var storeId = getSelectedRow();//选择一条
            if (storeId == null) {
                return;
            }
            vm.getStore(storeId);
            vm.showList = false;
            vm.title = "编辑商品";
        },
        add: function () {
            vm.store = {};
            vm.showList = false;
            vm.title = "增加商品";
        },
        saveOrUpdate: function () {
            var url = vm.store.id == null ? "store/saveStore" : "store/updateStore";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.store),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        detal: function(id) {

            $.ajax({
                type: "GET",
                url: baseURL + "order/detal/" + id,
                async: false,
                success: function (r) {
                    vm.orderDetails = r.orderDetails;
                }
            });
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "订单详情",
                area: ['450px', '700px'],
                shadeClose: true,
                content: jQuery("#detal"),
            });
        },

        del: function () {
            var storeIds = getSelectedRows();
            if (storeIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "store/delete",
                    contentType: "application/json",
                    data: JSON.stringify(storeIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getStore: function (storeId) {
            $.get(baseURL + "store/info/" + storeId, function (r) {
                vm.store = r.store;
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        }
    }
});

